package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.pj.shops.ShopActivity;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.qlearn.adapter.MyIntegeralAdapter;
import com.muqi.app.qlearn.modles.CreditInfo;
import com.muqi.app.qlearn.student.BaseStudentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.widget.pulltorefresh.XListView;
import com.muqi.app.widget.pulltorefresh.XListView.IXListViewListener;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MyIntegralActivity extends BaseStudentActivity implements
		IXListViewListener, OnClickListener {
	private XListView lv;
	private TextView tv_rule;
	private TextView tv_exchange;
	private TextView tv_remian;
	private TextView tv_goExcahnge;
	private TextView tv_home;
	private TextView tv_takein;
	private TextView tv_class;

	private int page = 1;
	private MyIntegeralAdapter adapter;
	private ArrayList<CreditInfo> infos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_integral);
	}

	@Override
	protected void onInit() {
		super.onInit();
		View headView = getLayoutInflater().inflate(
				R.layout.header_myintegeral, null);
		tv_class = (TextView) headView.findViewById(R.id.tv_integeral_class);
		tv_exchange = (TextView) headView
				.findViewById(R.id.tv_integeral_change);
		tv_goExcahnge = (TextView) headView
				.findViewById(R.id.tv_integeral_goexchange);
		tv_home = (TextView) headView.findViewById(R.id.tv_integeral_home);
		tv_remian = (TextView) headView.findViewById(R.id.tv_integeral_remian);
		tv_takein = (TextView) headView.findViewById(R.id.tv_integeral_takein);
		tv_rule = (TextView) findViewById(R.id.tv_rule);
		lv = (XListView) findViewById(R.id.integer_lv);
		lv.addHeaderView(headView);
		lv.setPullLoadEnable(false);
		lv.setPullRefreshEnable(false);
		lv.setXListViewListener(this);

		tv_rule.setOnClickListener(this);
		tv_goExcahnge.setOnClickListener(this);
		infos = new ArrayList<CreditInfo>();
		adapter = new MyIntegeralAdapter(infos, mContext);
		lv.setAdapter(adapter);
	}

	@Override
	protected void onData() {
		// TODO Auto-generated method stub
		super.onData();
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getInvitation();
		getInvitationList();
	}

	private void getInvitationList() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("user_id", mSpUtil.getCurrentChildId());
		sendMap.put("page", page);
		String requestUrl = ParamsUtils.buildParams(NetWorkApi.CREDIT_LIST_API,
				sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				Log.e("===", result);
				lv.stopRefresh();
				lv.setPullLoadEnable(false);
				if (resuleCode == RESULTCODE.SUCCESS) {
					ArrayList<CreditInfo> temp = ResponseUtils
							.getCreditInfoList(mContext, result);
					if (temp != null) {
						if (page == 1) {
							infos = temp;
							if (adapter != null)
								adapter = null;
							adapter = new MyIntegeralAdapter(infos, mContext);
							lv.setAdapter(adapter);
						} else {
							for (CreditInfo creditInfo : temp) {
								infos.add(creditInfo);
							}
							adapter.notifyDataSetChanged();
						}

						if (temp.size() < 10) {
							lv.setPullLoadEnable(false);
						} else {
							page++;
							lv.setPullLoadEnable(true);
						}

					}

				}

			}
		});

	}

	private void getInvitation() {

		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("user_id", mSpUtil.getCurrentChildId());
		String requestUrl = ParamsUtils.buildParams(NetWorkApi.CREDIT_TOP_API,
				sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				Log.e("===", result);
				if (resuleCode == RESULTCODE.SUCCESS) {
					try {
						JSONObject obj = new JSONObject(result);
						JSONObject dataOb = obj.getJSONObject("data");
						if (dataOb != null) {
							tv_remian.setText(dataOb
									.getString("credits_balance"));
							tv_exchange.setText(dataOb
									.getString("credits_used"));
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});
	}

	@Override
	public void onRefresh() {
		page = 1;
		getInvitation();
		getInvitationList();

	}

	@Override
	public void onLoadMore() {

		getInvitation();
		getInvitationList();
	}

	public void onBack(View view) {
		finish();
	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_rule:
			startActivity(IntegralRuleActivity.class);
			break;
		case R.id.tv_integeral_goexchange:
			startActivity(ShopActivity.class);
			break;
		default:
			break;
		}

	}

}
